﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SharpMath2;
using System.Reflection;
using Uniforax.Battlefield.Input;
using Uniforax.Battlefield.Interfaces;

namespace Uniforax.Battlefield.UI
{
    public class TestButton : DrawableGameComponent, IAceeptRoutedInput
    {
        private Texture2D _testButtonNormalTexture;
        private Texture2D _testButtonHoverTexture;
        private Texture2D _testButtonPressedTexture;

        private SpriteBatch _spriteBatch;

        private ButtonState _lastMouse;

        private static Vector2 _metaBoardSize = new Vector2(100, 100);
        private static Vector2[] _metaBoardPoints = [
            new Vector2( 8, 35),
            new Vector2( 9, 66),
            new Vector2(86, 62),
            new Vector2(84, 29)];

        //  51, 47

        private Polygon2 _boardPolygon;

        public Vector2 Position { get => _position; set => SetPosition(value); }
        private Vector2 _position;

        public Vector2 BoardSize { get => _boardSize; set => SetBoardSize(value); }
        private Vector2 _boardSize = _metaBoardSize;


        public TestButton(Game game) : base(game)
        {
            _boardPolygon = new Polygon2(_metaBoardPoints);
        }

        public override void Initialize()
        {
            base.Initialize();
        }
        protected override void LoadContent()
        {
            var testButtonNormalPath = @"TestButton\TestButton-Normal.png";
            var testButtonHoverPath = @"TestButton\TestButton-Hover.png";
            var testButtonPressedPath = @"TestButton\TestButton-Pressed.png";

            Game.Content.RootDirectory = Path.Combine(Environment.CurrentDirectory, "Assets");

            _testButtonNormalTexture = Game.Content.Load<Texture2D>(testButtonNormalPath);
            _testButtonHoverTexture = Game.Content.Load<Texture2D>(testButtonHoverPath);
            _testButtonPressedTexture = Game.Content.Load<Texture2D>(testButtonPressedPath);

            _spriteBatch = new SpriteBatch(GraphicsDevice);

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();

            _spriteBatch.Draw(_testButtonNormalTexture, new Rectangle((int)Position.X, (int)Position.Y, (int)BoardSize.X, (int)BoardSize.Y), Color.White);

            _spriteBatch.End();
            base.Draw(gameTime);
        }

        private void SetPosition(Vector2 value)
        {
            _position = value;
        }

        private void SetBoardSize(Vector2 value)
        {
            value = new Vector2(Math.Max(value.X, 1), Math.Max(value.Y, 1));
            UpdateBoardPolygon(value);
           
            _boardSize = value;
        }

        private void UpdateBoardPolygon(Vector2 value)
        {
            var metaBoardSize = _metaBoardSize;
            var metaBoardPoints = _metaBoardPoints;

            var newBoardPoints = metaBoardPoints
                .Select(i => new Vector2(i.X * value.X / metaBoardSize.X, i.Y * value.Y / metaBoardSize.Y))
                .ToArray();

            _boardPolygon = new Polygon2(newBoardPoints);
        }

        public void HandleInputReverse(RoutedEventArgs eventArgs)
        {

        }

        public void HandleInput(RoutedEventArgs eventArgs)
        {
            //var mouseState = Mouse.GetState();
            //if (mouseState.LeftButton == ButtonState.Pressed && _lastMouse == ButtonState.Released)
            //{

            //}

            //_lastMouse = mouseState.LeftButton;
        }

        public IEnumerable<IAceeptRoutedInput> GetInputChildren()
        {
            return Array.Empty<IAceeptRoutedInput>();
        }

        public void OnMouseLeftButtonDown(MouseRoutedEventArgs eventArgs)
        {

            Console.WriteLine($"Down {eventArgs.MouseState.X}, {eventArgs.MouseState.Y}");
        }

        public void OnMouseLeftButtonUp(MouseRoutedEventArgs eventArgs)
        {
            Console.WriteLine("Up");
        }

        public void OnMouseMove(MouseRoutedEventArgs eventArgs)
        {

        }

        public bool HitTest(Vector2 mousePos)
        {
            var isContain = Polygon2.Contains(_boardPolygon, Position, Rotation2.Zero, mousePos, false);
            //Console.WriteLine(isContain);
            return isContain;
        }
    }
}
